from django.shortcuts import render
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect, HttpResponseForbidden
from django.template import RequestContext
from datetime import datetime
from .forms import UserInfoForm
from system.models import UserInfo

def find(request):
	allData = UserInfo.objects.all();

	return render(request,
        'system/find.html',
        {
            'title':'Contact',
			'allData':allData
        })


def add(request):
	return render(request,
		'system/add.html',
		{
			'title':'Contact',
		})

def create(request):
	if request.method == 'POST':
		fo = UserInfoForm(request.POST)
		v = fo.is_valid()
		if v:
			user = UserInfo(name=fo.cleaned_data['name'],type=fo.cleaned_data['type'],create_data=fo.cleaned_data['create_data'],update_data=fo.cleaned_data['update_data'])
			name = fo.cleaned_data['name']
			user.save()
			return HttpResponse(name)
	else:
		fo = UserInfoForm()
	return render(request, 'userinfoform.html', {'form': form})



def update(request):
	name = request.GET['name']
	try:
	    data = UserInfo.objects.get(name=name);
	except :
	    data = UserInfo()
	return render(request,
        'system/update.html',
        {
            'title':'Contact',
			'data':data,
			'name':name
        })


def delete(request):
	return render(request,
        'system/add.html',
        {
            'title':'Contact',
        })


